<?php
/*
 * 商家的权限操作类
 * @package    Db
 * @author     Mark
 * @copyright  Copyright (c) 2013 ixdcw team
 * @version    $Id: 6a9a7792a710c14934f17a4b7547df3567b198ed $
 */
defined('IN_IXDCW') or exit('Access Denied');
final class BusinessAuthDb extends CoreDb{

	private $_memberDb;

	public function __construct(){
		parent::__construct();
		$this->_memberDb = Db('member');
		$this->_memberTable = Db('member')->tableName();
	}


	public function getByUserid($userid) {
		$sql = "select * from {$this->tableName()} where userid = $userid";
		return $this->_db->getOne($sql);
	}


	/**
	 * 清除不匹配的会员
	 * @return array|bool|mixed|mysqli_result|PDOStatement|string
	 */
	public function clearUnmatchedUser(){
		$num = $this->getCount();
		$countNum = intval($this->count($this->tableName()));
		if ($num !== $countNum) {
			$sqlUserid = "
				SELECT a.userid
				FROM {$this->tableName()} a, {$this->_memberTable} m
				WHERE a.userid =m.userid";
			$arrUserids = $this->_db->getCol($sqlUserid, 'userid');
			$sqlDeleteUnmatched = Sql::delete($this->tableName(), 'userid not in ('.implode(',', $arrUserids).')');
			return $this->_db->query($sqlDeleteUnmatched);
		}
		return true;
	}

	/**
	 * 获取会员列表
	 * @param string $condition
	 * @return array
	 */
	public function getList($condition = ''){
		$sqlList = "
			SELECT *
			FROM {$this->tableName()} a, {$this->_memberTable} m
			WHERE a.userid =m.userid";
		if ($condition) $sqlList .= $condition;

		$lists = array();
		$result  = $this->_db->query($sqlList);
		while ($r = $this->_db->fetchArray($result)) {
			$lists[] = $r;
		}
		return $lists;
	}

	/**
	 * 获取会员数量
	 * @param string $condition
	 * @return int
	 */
	public function getCount($condition = '') {
		$sqlMatchedNum = "
			SELECT COUNT(a.userid) num
			FROM {$this->tableName()} a, {$this->_memberTable} m
			WHERE a.userid =m.userid";
		if ($condition) $sqlMatchedNum .= $condition;
		return intval($this->_db->getVar($sqlMatchedNum, 'num'));
	}


	public function verify($userid) {
		global $DT_TIME;
		$item = $this->getByUserid($userid);
		if ($item) {
			$item['verify']        = 1;
			$item['verified_time'] = $DT_TIME;
			return $this->_db->query(Sql::update($this->tableName(), $item, "userid={$userid}"));
		} else {
			$item['userid']        = $userid;
			$item['verify']        = 1;
			$item['complate']      = 0;
			$item['verified_time'] = $DT_TIME;
			return $this->_db->query(Sql::insert($this->tableName(), $item));
		}
	}


	public function unVerify($userid) {
		global $DT_TIME;
		$sql  = "select * from {$this->tableName()} where userid = $userid";
		$item = $this->_db->getOne($sql);
		if ($item) {
			$item['verify']          = 0;
			$item['unverified_time'] = $DT_TIME;
			return $this->_db->query(Sql::update($this->tableName(), $item, "userid = {$userid}"));
		} else {
			$item['userid']          = $userid;
			$item['verify']          = 0;
			$item['complate']        = 0;
			$item['unverified_time'] = $DT_TIME;
			return $this->_db->query(Sql::insert($this->tableName(), $item));
		}
		return true;
	}

	/**
	 * 更新执照信息
	 */
	public function updateLicense($licenseUrl){
		global $_userid, $DT_TIME;
		$auth = $this->getByUserid($_userid);
		if ($auth) {
			$sql = "update ".$this->tableName()." set license_url='{$licenseUrl}', license_addtime = '$DT_TIME' where userid={$_userid}";
			ThumbUpload::clearPicture('', $licenseUrl, $auth['license_url'], $_userid);
			return $this->_db->query($sql);
		} else {
			$item = array(
				'userid'=> $_userid,
				'license_url'=> $licenseUrl,
				'license_addtime'=> $DT_TIME,
			);
			return $this->_db->query(Sql::insert($this->tableName(), $item));
		}
	}

	/**
	 * 更新license 状态
	 * @param $_userid
	 * @param $status
	 * @return array|bool|mixed|mysqli_result|PDOStatement|string
	 */
	public function updateLicenseStatus($_userid, $status) {
		$sql = "update " . $this->tableName() . " set license_status=".$status .' where userid='.$_userid;
		return $this->_db->query($sql);
	}
}